package com.lesso.lpq.sunflower.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lpq.sunflower.core.ApiResponse;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeDeviceDTO;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeDeviceQueryDTO;
import com.lesso.lpq.sunflower.pojo.vo.EmployeeDeviceVO;
import com.lesso.lpq.sunflower.service.IEmployeeDeviceService;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName : DeviceController
 * @Description :
 * @Author : 罗培棋
 * @Date: 2025-07-05 08:48
 */

@RestController
@RequestMapping("/rel/employee-device")
public class EmployeeDeviceController {

    @DubboReference
    private IEmployeeDeviceService employeeDeviceService;


    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody EmployeeDeviceDTO dto) {
        return ApiResponse.ok("success", employeeDeviceService.save(dto));
    }

    @GetMapping("{employeeId}")
    public ApiResponse<EmployeeDeviceVO> getByEmployeeId(@PathVariable String employeeId) {
        return ApiResponse.ok("SUCCESS", employeeDeviceService.getByEmployeeId(employeeId));
    }

    @GetMapping("/page")
    public ApiResponse<Page<EmployeeDeviceVO>> page(@RequestBody EmployeeDeviceQueryDTO dto) {
        return ApiResponse.ok("success", employeeDeviceService.page(dto));
    }


    @PutMapping("/{id}")
    public ApiResponse<Object> update(@PathVariable Long id, @RequestBody EmployeeDeviceDTO dto) {
        dto.setId(id); // 确保ID传入
        employeeDeviceService.update(dto);
        return ApiResponse.ok();
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<EmployeeDeviceVO>> customPage(EmployeeDeviceQueryDTO dto) {
        return ApiResponse.ok("success", employeeDeviceService.customPage(dto));
    }

    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "关系ID不能为空");
        if (id < 0) {
            throw new RuntimeException("关系ID不能小于0");
        }
        employeeDeviceService.deleteById(id);
        return ApiResponse.ok();
    }
}